package com.diy.sigmund.diyuser.mapper.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diy.sigmund.diycommon.spring.CustomApplicationContext;
import com.diy.sigmund.diyuser.common.middleware.easyexcel.ExportExcelListener;
import com.diy.sigmund.diyuser.common.middleware.easyexcel.ImportExcelListener;
import com.diy.sigmund.diyuser.entity.po.TestSql;
import com.diy.sigmund.diyuser.mapper.TestSqlMapper;
import com.diy.sigmund.diyuser.mapper.service.ITestSqlService;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ylm-sigmund
 * @since 2022-08-16
 */
@Service
public class TestSqlServiceImpl extends ServiceImpl<TestSqlMapper, TestSql> implements ITestSqlService {

    @Autowired
    private TestSqlMapper testSqlMapper;
    private static final Logger log = LoggerFactory.getLogger(TestSqlServiceImpl.class);

    @Override
    public void importExcel(MultipartFile file) throws IOException {
        long beforeTime = System.currentTimeMillis();
        EasyExcel.read(file.getInputStream(),
                        TestSql.class,
                        new ImportExcelListener(CustomApplicationContext.getBean(TestSqlServiceImpl.class)))
                .sheet().headRowNumber(1).doRead();
        long afterTime = System.currentTimeMillis();
        log.info("耗时:{}", afterTime - beforeTime);
    }

    @Override
    public void exportExcel(HttpServletResponse response) throws IOException {
        long beforeTime = System.currentTimeMillis();
        LambdaQueryWrapper<TestSql> queryWrapper = new LambdaQueryWrapper<>();

        new ExportExcelListener<TestSql>(testSqlMapper).
                exportNoQueryCount(response, "城市列表", TestSql.class,
                        queryWrapper);

        long afterTime = System.currentTimeMillis();
        log.info("耗时:{}", afterTime - beforeTime);
    }
}
